package com.andaily.domain.dto.cooking;

import com.andaily.domain.cooking.CookingAccount;
import com.andaily.domain.cooking.CookingAccountRepository;
import com.andaily.domain.cooking.CookingAccountStatus;
import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.user.User;
import com.andaily.web.context.BeanProvider;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class CookingAccountDto extends AbstractDTO {

    private static CookingAccountRepository repository() {
        return BeanProvider.getBean(CookingAccountRepository.class);
    }

    private String date;
    private String amount;
    private String creatorGuid;
    private String creatorName;

    private String description;
    private String status;
    private boolean allowEdit = true;

    public CookingAccountDto() {
        this.date = DateUtils.toDateText(DateUtils.now());
    }

    public CookingAccountDto(CookingAccount account) {
        this.guid = account.guid();
        this.date = DateUtils.toDateText(account.accountDate());
        User owner = account.owner();
        this.creatorGuid = owner.guid();
        this.creatorName = owner.nickName();

        this.description = account.description();
        this.amount = account.amount().toString();
        CookingAccountStatus accountStatus = account.status();
        this.status = accountStatus.getLabel();
        this.allowEdit = CookingAccountStatus.RECORDED.equals(accountStatus);
    }

    public boolean isAllowEdit() {
        return allowEdit;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getCreatorGuid() {
        return creatorGuid;
    }

    public void setCreatorGuid(String creatorGuid) {
        this.creatorGuid = creatorGuid;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCreatorName() {
        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public CookingAccount instance() {
        BigDecimal cAmount = new BigDecimal(this.amount);
        Date accountDate = DateUtils.getDate(this.date);
        CookingAccount account;
        if (isNewly()) {
            account = new CookingAccount(cAmount, accountDate, description);
        } else {
            account = repository().findByGuid(this.guid);
            account.update(cAmount, accountDate, description);
        }
        return account;
    }

    public static List<CookingAccountDto> toDtos(List<CookingAccount> accounts) {
        List<CookingAccountDto> list = new ArrayList<CookingAccountDto>(accounts.size());
        for (CookingAccount account : accounts) {
            list.add(new CookingAccountDto(account));
        }
        return list;
    }
}
